<include file="../header" />
<style>
    .keyword-input{
        margin-top: 1rem;
    }
    .keyword-btn{
        margin-top: 0.5rem;
        width: 120px;
        float: left;
    }
    .kw-btn{
        width: 50px;
    }
    .shut-down{
        width: 12px;
        height: 12px;
        position: relative;
        left: -0.5rem;
        top: -0.9rem;
        cursor: pointer;
    }
</style>
<script type="text/javascript" src="/Template/js/vue.min.js"></script>

<body class="J_scroll_fixed" id="container">
<script type="text/javascript">
    var rowNum = 0;
    function onlyNumber(obj){
        //得到第一个字符是否为负号
        var t = obj.value.charAt(0);
        //先把非数字的都替换掉，除了数字和.
        if(obj.value != 0)
        obj.value = obj.value.replace(/[^\d\.]/g,'').replace(/^0{1,}/g,'');
        //必须保证第一个为数字而不是.
        obj.value = obj.value.replace(/^\./g,'');
        //保证只有出现一个.而没有多个.
        obj.value = obj.value.replace(/\.{2,}/g,'.');
        //保证.只出现一次，而不能出现两次以上
        obj.value = obj.value.replace('.','$#$').replace(/\./g,'').replace('$#$','.');
        //只能输入两位小数
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
        //如果第一位是负号，则允许添加
        if(t == '-'){
            return;
        }
    }

    $(function() {
        function insertRows(coupon_code){
            var tb1 = $("#viewTabs");                // 找table的ID

            var product_name = $("select[name=product_id]").find("option:selected").text();
            var size = $("select[name=size]").val();
            var number = $("input[name=number]").val();
            var price = $("input[name=price]").val();
            var default_doctor_name = $("input[name=default_doctor_name]").val();
            var default_hospital_name = $("input[name=default_hospital_name]").val();

            var $td0 = $("<td></td>");
            $td0.html(coupon_code);
            var $td1 = $("<td></td>");
            $td1.html(product_name);
            var $td2 = $("<td></td>");
            $td2.html(size);
            var $td3 = $("<td></td>");
            $td3.html(number);
            var $td4 = $("<td></td>");
            $td4.html(price);
            var $td5 = $("<td></td>");
            $td5.html(default_doctor_name + "（" + default_hospital_name + "）");

            var $tr = $("<tr id=\'_tr_"+rowNum+"\' class=\'in-opt\'></tr>");// 创建tr，将3个td放置到tr中
            $tr.append($td0);
            $tr.append($td1);
            $tr.append($td2);
            $tr.append($td3);
            $tr.append($td4);
            $tr.append($td5);
            //在表格的最后追加新增的tr
            tb1.append($tr);

            $(".count").show();
            rowNum++;
            $("input[name=row]").val(rowNum);
        }

        $(".addCoupon").click(function() {
           var product_id = $("select[name=product_id]").val();
            var size = $("select[name=size]").val();
            var price = $("input[name=price]").val();
            var number = $("input[name=number]").val();
//            var need_doctor = $("select[name=need_doctor]").val();
            var coupon_hospital_id = $("select[name=coupon_hospital_id]").val();
            var default_doctor_name = $("input[name=default_doctor_name]").val();
            var default_hospital_name = $("input[name=default_hospital_name]").val();

            if (product_id == 0) {
                alert("请选择【产品】");
                $("select[name=product_id]").focus();
                return false;
            }
            if (coupon_hospital_id == 0) {
                alert("请选择【渠道】");
                $("select[name=coupon_hospital_id]").focus();
                return false;
            }
            if (size == "") {
                alert("请选择【规格】");
                $("select[name=size]").focus();
                return false;
            }
            if (price == "") {
                alert("请填写【单价】");
                $("input[name=price]").focus();
                return false;
            }
            if (number == "") {
                alert("请填写【数量】");
                $("input[name=number]").focus();
                return false;
            }
//            if (need_doctor == 1) {
//                if (default_doctor_name == "") {
//                    alert("请填写医生");
//                    $("input[name=default_doctor_name]").focus();
//                    return false;
//                }
//                if (default_hospital_name == "") {
//                    alert("请填写医院");
//                    $("input[name=default_hospital_name]").focus();
//                    return false;
//                }
//            }

            var data = {};
            data.product_id = product_id;
            data.coupon_hospital_id = coupon_hospital_id;
            data.size = size;
            data.price = price;
            data.number = number;
//            data.need_doctor = need_doctor;
            data.default_doctor_name = default_doctor_name;
            data.default_hospital_name = default_hospital_name;
            $.ajax({
                url:"/admin/coupon/addPost",
                type:"post",
                data:data,
                dataType:"JSON",
                beforeSend:function(){
                    $("#loadingToast").show();
                },
                complete:function(){
                    $('#loadingToast').hide();
                },
                success:function(data){
                    if(data.code != 200) {
                        alert(data.msg);
                        return false;
                    }
                    insertRows(data.coupon_code);
                    alert("添加成功：" + data.coupon_code);
                    return false;
//                    window.location.href = "/wechat/userCenter/payInfo/id/"+data.order_id;
                },
            })
            return false;
        });

        $("select[name='product_id']").change(function () {
            getSize();
        });

        $("select[name='coupon_hospital_id']").change(function () {
            $("input[name=default_hospital_name]").val($("select[name='coupon_hospital_id']").find("option:selected").text());
        });

        function getSize() {
            $("select[name=size]").empty();
            $("select[name=size]").append("<option value=''></option>");
            var productId = $("select[name=product_id]").val();
            var listArr=<?php echo json_encode($listProduct);?>;
            var list=eval(listArr);
            for(var i=0;i<list.length;i++){
                if(list[i]["product_id"] == productId){
                    $("img[name=preShow]").attr("src", list[i]["image_url"]);
                    $("#supplier").text("[ " + list[i]["supplier_name"] + " ]");
                    $("input[name=price]").val(list[i]["coupon_price"]);
                    $("input[name=default_doctor_name]").val(list[i]["default_doctor_name"]);
                    $("input[name=default_hospital_name]").val(list[i]["default_hospital_name"]);

                    var sizeArr = list[i].size.split("/");
                    for(var j=0;j<sizeArr.length;j++){
                        $("select[name=size]").append("<option value='"+sizeArr[j]+"'>" + sizeArr[j] + "</option>");
                    }
                    break;
                }
            }
        };

        getSize();

    })

</script>
<style type="text/css">
    .btn-danger,.btn-info,.btn-success,.btn-warning{
        width: 50px;
        /*height: 18px;*/
        padding: 2px 0px 2px 0px;
        margin: 1px 0px 1px 0px;
    }
    .price{
        width: 70px;
    }
</style>
<script type="text/javascript"  src="__TMP__/assets/js/ueditor/ueditor.config.js"></script>
<script type="text/javascript"  src="__TMP__/assets/js/ueditor/ueditor.all.min.js"></script>
<div class="wrap jj">
    <div class="common-form">
        <form method="post" class="form-horizontal J_ajaxForm">
            <fieldset>
                <!--div class="control-group">
                    <label class="control-label">CP码：</label>
                    <div class="controls">
                        <input type="text" name="coupon_code" value="{$randomCode}" readonly/>
                    </div>
                </div-->
                <div class="control-group">
                    <label class="control-label">产品：</label>
                    <div class="controls">
                        <select name="product_id">
                            {:getCommonSelectOptionHtml(2, "Product", "product_id", "product_name", null, "coupon_product", 0)}
                        </select>
                        &nbsp;
                        <a style="color: #8b8b8b" id="supplier"></a>
                    </div>
                    <br/>
                    <div class="controls">
                        <img src="" name="preShow" style="height:200px;" />
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">渠道：</label>
                    <div class="controls">
                        <select name="coupon_hospital_id">
                            {:getCommonSelectOptionHtml(0, "CouponHospital", "coupon_hospital_id", "coupon_hospital_name", null, null, 1, "")}
                        </select>
                    </div>
                    <br/>
                </div>
                <div class="control-group">
                    <label class="control-label">规格：</label>
                    <div class="controls">
                        <select name="size">
                            <option value=''></option>
                        </select>
                    </div>
                </div>
                <!--div class="control-group">
                    <label class="control-label">需要医生：</label>
                    <div class="controls">
                        <select name="need_doctor" style="width: auto">
                            <option value="">选择</option>
                            <option value="1" selected="selected">是</option>
                            <option value="0">否</option>
                        </select>
                    </div>
                </div-->

                <div class="control-group doc">
                    <label class="control-label">默认医生：</label>
                    <div class="controls">
                        <input type="text" class="input" name="default_doctor_name">
                    </div>
                </div>
                <div class="control-group doc">
                    <label class="control-label">默认医院：</label>
                    <div class="controls">
                        <input type="text" class="input" name="default_hospital_name">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">单价：</label>
                    <div class="controls">
                        <input type="text" class="input" name="price" value="3000"  onkeyup="onlyNumber(this)">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">数量：</label>
                    <div class="controls">
                        <input type="text" class="input" name="number" value="1"  onkeyup="onlyNumber(this)">
                    </div>
                </div>

                <hr class="half-rule">
                <div class="control-group count" style="display: none">
                    <label class="control-label">已添加：</label>
                    <div class="controls">
                        <table class="table table-hover table-bordered" id="viewTabs" style="width: 600px">
                            <thead>
                            <tr>
                                <th style="min-width: 100px" width="100px">CP码</th>
                                <th style="min-width: 150px" width="150px">产品名称</th>
                                <th style="min-width: 30px" width="30px">规格</th>
                                <th style="min-width: 30px" width="30px">数量</th>
                                <th style="min-width: 30px" width="30px">单价</th>
                                <th style="min-width: 80px" width="200px">医生（医院）</th>
                            </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                        统计：<input type="text" style="width: 50px" name="row" value="{$row}" readonly/>
                    </div>
                </div>
            </fieldset>
            <div class="form-actions">
                <button type="submit" class="btn btn-primary addCoupon">添加</button>
                <a class="btn" href="__URL__">返回</a>
            </div>
        </form>
    </div>
</div>
<script src="__ROOT__/statics/js/common.js"></script>
</body>
</html>